package com.tencent.mm.opensdk.modelmsg;

import android.graphics.Bitmap;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;

import java.io.ByteArrayOutputStream;
import java.io.OutputStream;

public final class WXMediaMessage {
    public static class Builder {
        public static final String KEY_IDENTIFIER = "_wxobject_identifier_";

        public Builder() {

        }

        public static WXMediaMessage fromBundle(Bundle arg6) {
            WXMediaMessage v0_1;
            WXMediaMessage v1 = new WXMediaMessage();
            v1.sdkVer = arg6.getInt("_wxobject_sdkVer");
            v1.title = arg6.getString("_wxobject_title");
            v1.description = arg6.getString("_wxobject_description");
            v1.thumbData = arg6.getByteArray("_wxobject_thumbdata");
            v1.mediaTagName = arg6.getString("_wxobject_mediatagname");
            v1.messageAction = arg6.getString("_wxobject_message_action");
            v1.messageExt = arg6.getString("_wxobject_message_ext");
            String v2 = Builder.pathOldToNew(arg6.getString("_wxobject_identifier_"));
            if (v2 != null && v2.length() > 0) {
                try {
                    v1.mediaObject = (IMediaObject) Class.forName(v2).newInstance();
                    v1.mediaObject.unserialize(arg6);
                    v0_1 = v1;
                } catch (Exception v0) {
                    Log.e("MicroMsg.SDK.WXMediaMessage", "get media object from bundle failed: unknown ident " + v2 + ", ex = " + v0.getMessage());
                    v0_1 = v1;
                }
            } else {
                v0_1 = v1;
            }

            return v0_1;
        }

        private static String pathNewToOld(String arg2) {
            if (arg2 == null || arg2.length() == 0) {
                Log.e("MicroMsg.SDK.WXMediaMessage", "pathNewToOld fail, newPath is null");
            } else {
                arg2 = arg2.replace("com.tencent.mm.opensdk.modelmsg", "com.tencent.mm.sdk.openapi");
            }

            return arg2;
        }

        private static String pathOldToNew(String arg2) {
            if (arg2 == null || arg2.length() == 0) {
                Log.e("MicroMsg.SDK.WXMediaMessage", "pathOldToNew fail, oldPath is null");
            } else {
                arg2 = arg2.replace("com.tencent.mm.sdk.openapi", "com.tencent.mm.opensdk.modelmsg");
            }

            return arg2;
        }

        public static Bundle toBundle(WXMediaMessage arg3) {
            Bundle v0 = new Bundle();
            v0.putInt("_wxobject_sdkVer", arg3.sdkVer);
            v0.putString("_wxobject_title", arg3.title);
            v0.putString("_wxobject_description", arg3.description);
            v0.putByteArray("_wxobject_thumbdata", arg3.thumbData);
            if (arg3.mediaObject != null) {
                v0.putString("_wxobject_identifier_", Builder.pathNewToOld(arg3.mediaObject.getClass().getName()));
                arg3.mediaObject.serialize(v0);
            }

            v0.putString("_wxobject_mediatagname", arg3.mediaTagName);
            v0.putString("_wxobject_message_action", arg3.messageAction);
            v0.putString("_wxobject_message_ext", arg3.messageExt);
            return v0;
        }
    }

    public interface IMediaObject {
        public static final int TYPE_APPBRAND = 33;
        public static final int TYPE_APPDATA = 7;
        public static final int TYPE_CARD_SHARE = 16;
        public static final int TYPE_DESIGNER_SHARED = 25;
        public static final int TYPE_DEVICE_ACCESS = 12;
        public static final int TYPE_EMOJI = 8;
        public static final int TYPE_EMOJILIST_SHARED = 27;
        public static final int TYPE_EMOTICON_GIFT = 11;
        public static final int TYPE_EMOTICON_SHARED = 15;
        public static final int TYPE_EMOTIONLIST_SHARED = 26;
        public static final int TYPE_FILE = 6;
        public static final int TYPE_GIFTCARD = 34;
        public static final int TYPE_IMAGE = 2;
        public static final int TYPE_LOCATION = 30;
        public static final int TYPE_LOCATION_SHARE = 17;
        public static final int TYPE_MALL_PRODUCT = 13;
        public static final int TYPE_MUSIC = 3;
        public static final int TYPE_NOTE = 24;
        public static final int TYPE_OLD_TV = 14;
        public static final int TYPE_PRODUCT = 10;
        public static final int TYPE_RECORD = 19;
        public static final int TYPE_TEXT = 1;
        public static final int TYPE_TV = 20;
        public static final int TYPE_UNKNOWN = 0;
        public static final int TYPE_URL = 5;
        public static final int TYPE_VIDEO = 4;

        boolean checkArgs();

        void serialize(Bundle arg1);

        int type();

        void unserialize(Bundle arg1);
    }

    public static final String ACTION_WXAPPMESSAGE = "com.tencent.mm.sdk.openapi.Intent.ACTION_WXAPPMESSAGE";
    private static final int DESCRIPTION_LENGTH_LIMIT = 1024;
    private static final int MEDIA_TAG_NAME_LENGTH_LIMIT = 64;
    private static final int MESSAGE_ACTION_LENGTH_LIMIT = 2048;
    private static final int MESSAGE_EXT_LENGTH_LIMIT = 2048;
    private static final String TAG = "MicroMsg.SDK.WXMediaMessage";
    public static final int THUMB_LENGTH_LIMIT = 32768;
    private static final int TITLE_LENGTH_LIMIT = 512;
    public String description;
    public IMediaObject mediaObject;
    public String mediaTagName;
    public String messageAction;
    public String messageExt;
    public int sdkVer;
    public byte[] thumbData;
    public String title;

    public WXMediaMessage() {
        this(null);
    }

    public WXMediaMessage(IMediaObject arg1) {

        this.mediaObject = arg1;
    }

    final boolean checkArgs() {
        int v3 = 2048;
        boolean v0 = false;
        if (this.getType() == 8) {
            if (this.thumbData != null && this.thumbData.length != 0) {

            } else {
                Log.e("MicroMsg.SDK.WXMediaMessage", "checkArgs fail, thumbData should not be null when send emoji");
                return false;
            }
        }
        label_14:
        if (this.thumbData != null && this.thumbData.length > 32768) {
            Log.e("MicroMsg.SDK.WXMediaMessage", "checkArgs fail, thumbData is invalid");
            return v0;
        }

        if (this.title != null && this.title.length() > 512) {
            Log.e("MicroMsg.SDK.WXMediaMessage", "checkArgs fail, title is invalid");
            return v0;
        }

        if (this.description != null && this.description.length() > 1024) {
            Log.e("MicroMsg.SDK.WXMediaMessage", "checkArgs fail, description is invalid");
            return v0;
        }

        if (this.mediaObject == null) {
            Log.e("MicroMsg.SDK.WXMediaMessage", "checkArgs fail, mediaObject is null");
            return v0;
        }

        if (this.mediaTagName != null && this.mediaTagName.length() > 64) {
            Log.e("MicroMsg.SDK.WXMediaMessage", "checkArgs fail, mediaTagName is too long");
            return v0;
        }

        if (this.messageAction != null && this.messageAction.length() > v3) {
            Log.e("MicroMsg.SDK.WXMediaMessage", "checkArgs fail, messageAction is too long");
            return v0;
        }

        if (this.messageExt != null && this.messageExt.length() > v3) {
            Log.e("MicroMsg.SDK.WXMediaMessage", "checkArgs fail, messageExt is too long");
            return v0;
        }

        v0 = this.mediaObject.checkArgs();
        return v0;
    }

    public final int getType() {
        int v0 = this.mediaObject == null ? 0 : this.mediaObject.type();
        return v0;
    }

    public final void setThumbImage(Bitmap arg5) {
        try {
            ByteArrayOutputStream v0_1 = new ByteArrayOutputStream();
            arg5.compress(Bitmap.CompressFormat.JPEG, 85, ((OutputStream) v0_1));
            this.thumbData = v0_1.toByteArray();
            v0_1.close();
        } catch (Exception v0) {
            Log.e("MicroMsg.SDK.WXMediaMessage", "setThumbImage exception:" + v0.getMessage());
        }
    }
}

